//
//  NSObject+UIUtil.m
//  DigitalGeek
//
//  Created by lienbao on 10/09/2014.
//  Copyright (c) 2014 com.lienbao.digitalgeek. All rights reserved.
//

#import "Utility.h"

@implementation Utility

+ (CLLocationDistance)distanceFrom:(CLLocationCoordinate2D)coordinate1 toCoordinate:(CLLocationCoordinate2D)coordinate2
{
    CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:coordinate1.latitude longitude:coordinate1.longitude];
    CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:coordinate2.latitude longitude:coordinate2.longitude];
    return [loc1 distanceFromLocation:loc2];
}

+ (BOOL)isValidCoordinate:(CLLocationCoordinate2D)coordinate
{
    if (CLLocationCoordinate2DIsValid(coordinate)) {
        if (fabs(coordinate.latitude)   != 0.0   &
            fabs(coordinate.latitude)   != 90.0  &
            fabs(coordinate.longitude)  != 0.0   &
            fabs(coordinate.longitude)  != 180.0  ) {
            return YES;
        }
    }
    return NO;
}

+ (BOOL)isValidateEmail:(NSString *)email
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}


+ (BOOL)isValidateMobile:(NSString *)mobile
{
    //手机号以13，14, 15，17, 18开头，八个 \d 数字字符
    NSString *phoneRegex = @"^[1][3,4,5,7,8][0-9]{9}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:mobile];
}

+ (CLLocationCoordinate2D)googleToBaiduCoordinate:(CLLocationCoordinate2D)coordinate
{
    return CLLocationCoordinate2DMake(coordinate.latitude + 0.006, coordinate.longitude + 0.0065);
}

+ (CLLocationCoordinate2D)baiduToGoogleCoordinate:(CLLocationCoordinate2D)coordinate
{
    return CLLocationCoordinate2DMake(coordinate.latitude - 0.006, coordinate.longitude - 0.0065);
}

@end
